package com.beiye.drivertransport.specifiedLearning.business;


import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;

import com.beiye.drivertransport.SubActivity.ClipImageActivity;
import com.beiye.drivertransport.SubActivity.SignActivity;
import com.beiye.drivertransport.SubActivity.SignCourseActivity;

import com.beiye.drivertransport.adapter.ListBaseAdapter;
import com.beiye.drivertransport.adapter.SuperViewHolder;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.bean.MineUseBean;
import com.beiye.drivertransport.bean.SpecifiedLearnIngHomeBean;
import com.beiye.drivertransport.bean.UpPhotoBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.fragment.TwoBaseFgt;
import com.beiye.drivertransport.http.Login;


import com.beiye.drivertransport.utils.CameraCanUseUtils;
import com.beiye.drivertransport.utils.FileUtil;
import com.beiye.drivertransport.utils.HelpUtil;
import com.beiye.drivertransport.utils.LogUtils;
import com.beiye.drivertransport.utils.TakePhotoDialog;
import com.beiye.drivertransport.utils.ToastUtil;
import com.beiye.drivertransport.utils.UserManger;
import com.beiye.drivertransport.utils.Utils;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
//企业管理员自主培训
public class CourseSupermarketBusinessFragment extends TwoBaseFgt {
    private int firstIndex = 1;
    private int pageSize = 15;

    View empty_view;

    LRecyclerView lv_course;
    private LRecyclerViewAdapter lRecyclerViewAdapter1;
    private SpecifiedLearningHomeApt specifiedLearningHomeApt;
    //请求访问外部存储
    private static final int READ_EXTERNAL_STORAGE_REQUEST_CODE = 103;
    //请求写入外部存储
    private static final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 104;
    private File tempFile;
    //请求相机
    private static final int REQUEST_CAPTURE = 100;
    //请求相册
    private static final int REQUEST_PICK = 101;
    //请求截图
    private static final int REQUEST_CROP_PHOTO = 102;
    private int type;
    private int minPer;
    private int faceRecgMark;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_course_supermarket_business;
    }


    public void onClick(View view) {

    }

    @Override
    public void initData() {
        initUi();
    }

    @Override
    public void requestData() {
        String orgId = getArguments().getString("orgId");
        minPer = getArguments().getInt("minPer");
        faceRecgMark = getArguments().getInt("faceRecgMark");
        int photoMark = getArguments().getInt("photoMark");
        SharedPreferences sp = getActivity().getSharedPreferences("CourseSupermarketBusinessFragment",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("orgId", orgId);
        editor.putInt("photoMark", photoMark);
        editor.commit();
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        String userId = data.getUserId();
        new Login().getSpecifiedLearningTwo(orgId, userId, 6, 0, firstIndex, pageSize, this, 1);
        usefacephoto();//判断是否有头像

    }

    private void usefacephoto() {
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        String userId = data.getUserId();
        new Login().getUser(userId, this, 2);
    }

    @Override
    public void onUserVisible() {

    }

    private void showTakePhotoPopupWindow() {
        TakePhotoDialog.Builder builder = new TakePhotoDialog.Builder(getActivity());
        builder.setTitle("为方便后台管理员审核您的学习");
        builder.setMessage("请上传您的个人照片");
        builder.setPositiveButton("从手机中选择照片", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //权限判断
                if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    //申请READ_EXTERNAL_STORAGE权限
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                            READ_EXTERNAL_STORAGE_REQUEST_CODE);
                } else {
                    //跳转到相册
                    gotoPhoto();
                }

                dialog.dismiss();

                //设置你的操作事项
            }
        });
        builder.setPositiveButton1("拍摄照片", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //权限判断
                if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    //申请WRITE_EXTERNAL_STORAGE权限
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
                } else {
                    //跳转到调用系统相机

                    if (!CameraCanUseUtils.isCameraCanUse()) {
                        ToastUtil.showShortToast(getActivity(), "请到手机设置界面里找驾运宝允许开启照相");
                        return;
                    }

                    gotoCamera();
                }
                dialog.dismiss();

                //设置你的操作事项
            }
        });
        builder.setNegativeButton("跳过", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //设置你的操作事项
            }
        });
        builder.create().show();
    }

    /**
     * 跳转到相册
     */
    private void gotoPhoto() {
        Log.d("evan", "*****************打开图库********************");
        //跳转到调用系统图库
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(Intent.createChooser(intent, "请选择图片"), REQUEST_PICK);
    }

    /**
     * 跳转到照相机
     */
    private void gotoCamera() {
        Log.d("evan", "*****************打开相机********************");
        //创建拍照存储的图片文件
        tempFile = new File(FileUtil.checkDirPath(Environment.getExternalStorageDirectory().getPath() + "/image/"), System.currentTimeMillis() + ".jpg");

        //跳转到调用系统相机
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //设置7.0中共享文件，分享路径定义在xml/file_paths.xml
            intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(getContext(), com.beiye.drivertransport.BuildConfig.APPLICATION_ID + ".fileProvider", tempFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
        } else {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
        }
        startActivityForResult(intent, REQUEST_CAPTURE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        switch (requestCode) {
            case REQUEST_CAPTURE: //调用系统相机返回
                if (resultCode == Activity.RESULT_OK) {
                    gotoClipActivity(Uri.fromFile(tempFile));
                }
                break;
            case REQUEST_PICK:  //调用系统相册返回
                if (resultCode == Activity.RESULT_OK) {
                    Uri uri = intent.getData();
                    gotoClipActivity(uri);
                }
                break;
            case REQUEST_CROP_PHOTO:  //剪切图片返回
                if (resultCode == Activity.RESULT_OK) {
                    final Uri uri = intent.getData();
                    if (uri == null) {
                        return;
                    }
                    String cropImagePath = FileUtil.getRealFilePathFromUri(getActivity(), uri);
                    File file = new File(cropImagePath);
                    LoginUserBean userInfo = UserManger.getUserInfo();
                    LoginUserBean.DataBean data = userInfo.getData();
                    String userId = data.getUserId();
                    uploadImg(file, userId);
                }
                break;
        }
    }

    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    private final OkHttpClient client = new OkHttpClient();

    private void uploadImg(File mediaFile, String userId) {
        // mImgUrls为存放图片的url集合
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

        if (mediaFile != null) {
            builder.addFormDataPart("headPhoto", mediaFile.getName(), RequestBody.create(MEDIA_TYPE_PNG, mediaFile));
            builder.addFormDataPart("userId", userId);
        }
        MultipartBody requestBody = builder.build();
        //构建请求
        Request request = new Request.Builder()
                .url(AppInterfaceConfig.BASE_URL + "wx/uploadOfHeadPhoto")//地址
                .post(requestBody)//添加请求体
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                LogUtils.e("上传失败", e.getLocalizedMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                LogUtils.e("上传成功", str);
                UpPhotoBean upPhotoBean = JSON.parseObject(str, UpPhotoBean.class);
                String data = upPhotoBean.getData();
                Looper.prepare();
                usefacephoto(); //从新查询人脸识别要求上传头像
                Looper.loop();
                LogUtils.e("上传图片", data);

            }
        });

    }

    /**
     * 外部存储权限申请返回
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                gotoCamera();
            }
        } else if (requestCode == READ_EXTERNAL_STORAGE_REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                gotoPhoto();
            }
        }
    }

    /**
     * 打开截图界面
     */
    public void gotoClipActivity(Uri uri) {
        if (uri == null) {
            return;
        }
        Intent intent = new Intent();
        intent.setClass(getContext(), ClipImageActivity.class);
        intent.putExtra("type", type);
        intent.setData(uri);
        startActivityForResult(intent, REQUEST_CROP_PHOTO);
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if (what == 1) {
            SpecifiedLearnIngHomeBean courseSuperBean = JSON.parseObject(result, SpecifiedLearnIngHomeBean.class);
            if (courseSuperBean != null) {
                try {
                    if (courseSuperBean.getRows() != null && courseSuperBean.getRows().size() > 0) {
                        lv_course.setVisibility(View.VISIBLE);
                        if (firstIndex == 1) {
                            specifiedLearningHomeApt.clear();
                            specifiedLearningHomeApt.setDataList(courseSuperBean.getRows());
                        } else {
                            specifiedLearningHomeApt.addAll(courseSuperBean.getRows());
                        }

                        if (courseSuperBean.getRows().size() < pageSize) {
                            lv_course.setNoMore(true);
                        }
                    } else {
                        if (firstIndex == 1) {//第一页
                            lv_course.setEmptyView(empty_view);
                            specifiedLearningHomeApt.clear();
                        } else {
                            lv_course.setNoMore(true);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                lv_course.refreshComplete(courseSuperBean.getRows() == null ? 0 : courseSuperBean.getRows().size());
                lRecyclerViewAdapter1.notifyDataSetChanged();
            }
        } else if (what == 2) {
            MineUseBean mineUseBean = JSON.parseObject(result, MineUseBean.class);
            MineUseBean.DataBean data = mineUseBean.getData();
            String photoUrl = data.getPhotoUrl();
            if (photoUrl == null) {

                SharedPreferences sp = getContext().getSharedPreferences("CourseSupermarketBusinessFragment", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("photoUrl", "");
                edit.commit();

            } else {
                SharedPreferences sp = getContext().getSharedPreferences("CourseSupermarketBusinessFragment", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("photoUrl", photoUrl);
                edit.commit();
            }
        }

        super.onSuccess(result, call, response, what);

    }

    @Override
    public void onResume() {
        lv_course.refresh();
        requestData();
        super.onResume();
    }

    private void initUi() {
        LinearLayoutManager ms = new LinearLayoutManager(getActivity());
        ms.setOrientation(LinearLayoutManager.VERTICAL);
        lv_course.setLayoutManager(ms);
        specifiedLearningHomeApt = new SpecifiedLearningHomeApt(getActivity());

        lRecyclerViewAdapter1 = new LRecyclerViewAdapter(specifiedLearningHomeApt);
        lv_course.setAdapter(lRecyclerViewAdapter1);
        lv_course.setFooterViewColor(R.color.colorAccent, R.color.dark, android.R.color.white);
        lv_course.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        lv_course.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh() {
                firstIndex = 1;
                requestData();
            }
        });

        lv_course.refresh();
        lv_course.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void onLoadMore() {
                firstIndex += pageSize;
                requestData();
            }
        });
        empty_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lv_course.refresh();
            }
        });

        lv_course.refresh();

    }


    public class SpecifiedLearningHomeApt extends ListBaseAdapter<SpecifiedLearnIngHomeBean.RowsBean> {
        protected Context context = null;

        public SpecifiedLearningHomeApt(Context context) {
            super(context);
            this.context = context;
        }

        @Override
        public int getLayoutId() {
            return R.layout.specifiedlearning_item_layout1;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            final SpecifiedLearnIngHomeBean.RowsBean rowsBean = getDataList().get(position);

            TextView tv_chNo = holder.getView(R.id.tv_chNo);
            TextView tv_title = holder.getView(R.id.tv_title);
            TextView tv_yes = holder.getView(R.id.tv_parent4);//完成情况
            TextView tv_sign = holder.getView(R.id.tv_parent8);//签名情况
            TextView tv_check = holder.getView(R.id.tv_parent5);//审核情况
            TextView tv_Spotcheck = holder.getView(R.id.tv_parent7);//抽查情况
            TextView tv_actualChLength = holder.getView(R.id.tv_time);//实际学习时间
            TextView tv_date = holder.getView(R.id.tv_date);
            TextView tv_line_specified = holder.getView(R.id.tv_line_specified);//背景条
            final TextView tv_specified_learning = holder.getView(R.id.tv_specified_learning);//学习/签名

            RelativeLayout re_specified1 = holder.getView(R.id.re_specified1);
            final String chName = rowsBean.getChName();
            final int chNo = rowsBean.getChNo();
            final int finishMark = rowsBean.getFinishMark();
            long beginDate = rowsBean.getBeginDate();
            long endDate = rowsBean.getEndDate();
            Date date1 = new Date(beginDate);
            Date date2 = new Date(endDate);
            String begin = "";
            String end = "";
            try {
                //yyyy表示年MM表示月dd表示日
                //yyyy-MM-dd是日期的格式，比如2015-12-12如果你要得到2015年12月12日就换成yyyy年MM月dd日
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
                //进行格式化
                begin = sdf.format(date1);
                end = sdf.format(date2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            tv_date.setText(begin + "  —  " + end);

            re_specified1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClicker()) {
                        return;
                    }
                    SharedPreferences sp = getActivity().getSharedPreferences("CourseSupermarketBusinessFragment",
                            Context.MODE_PRIVATE);
                    int photoMark = sp.getInt("photoMark", 0);
                    String photoUrl = sp.getString("photoUrl", "");
                    String orgId = sp.getString("orgId", "");
                    if (photoUrl.equals("")) {
                        showTakePhotoPopupWindow();
                        return;
                    }
                    int sn = rowsBean.getSn();
                    int setChLength = 0;
                    setChLength = (rowsBean.getMiNo() > rowsBean.getSetChLength()) ? rowsBean.getMiNo() : rowsBean.getSetChLength();
                    int checkMark = rowsBean.getCheckMark();
                    Integer qpSn = rowsBean.getQpSn();
                    Integer otpSn = HelpUtil.judgeIntNull(rowsBean.getOtpSn());
                    Bundle bundle = new Bundle();
                    bundle.putInt("photoMark", photoMark);
                    bundle.putInt("sn", sn);
                    bundle.putString("chName", chName);
                    bundle.putInt("setChLength", setChLength);
                    bundle.putInt("checkMark", checkMark);
                    bundle.putInt("finishMark", finishMark);
                    bundle.putInt("faceRecgMark", faceRecgMark);
                    bundle.putString("photoUrl", photoUrl);
                    bundle.putString("orgId", orgId);
                    bundle.putInt("chNo", chNo);
                    bundle.putInt("otpSn", otpSn);
                    bundle.putInt("minPer", minPer);
                    if (qpSn == null) {
                        bundle.putInt("qpSn", 0);
                    } else {

                        bundle.putInt("qpSn", qpSn);
                    }
                    startActivity(SubCourseLearnBusinessItemActivity.class, bundle);
                }


            });

            if (chNo == 0) {
                tv_chNo.setText("学时");
            } else {
                tv_chNo.setText(chNo + "学时");
            }
            if (chName == null) {
                tv_title.setText("");
            } else {
                tv_title.setText(chName);
            }

            final Integer qpSn = rowsBean.getQpSn();
            if (finishMark == 0) {//未完成
                int actualChLength = rowsBean.getActualChLength();
                int setChLength = 0;
                setChLength = (rowsBean.getMiNo() > rowsBean.getSetChLength()) ? rowsBean.getMiNo() : rowsBean.getSetChLength();
                if (actualChLength == 0 || setChLength == 0) {
                    tv_actualChLength.setText("0%");
                    tv_actualChLength.setTextColor(getActivity().getResources().getColor(R.color.bixue_red));
                    tv_specified_learning.setText("学习");
                    tv_line_specified.setBackgroundResource(R.color.specified_red);
                    tv_yes.setText("未完成");
                    tv_yes.setBackgroundResource(R.drawable.shape_specified_nored);
                    tv_check.setVisibility(View.GONE);
                    tv_sign.setVisibility(View.GONE);
                } else {
                    float studyper = (float) actualChLength / (float) setChLength;
                    if (studyper < 1) {
                        int studyper1 = (int) (studyper * 100);
                        //  String ss = String.format("%1.2f", studyper1);
                        tv_actualChLength.setText(studyper1 + "%");
                        tv_actualChLength.setTextColor(getActivity().getResources().getColor(R.color.bixue_red));
                        tv_specified_learning.setText("学习");
                        tv_line_specified.setBackgroundResource(R.color.specified_red);
                        tv_yes.setText("未完成");
                        tv_yes.setBackgroundResource(R.drawable.shape_specified_nored);
                        tv_check.setVisibility(View.GONE);
                        tv_sign.setVisibility(View.GONE);

                    } else {//未完成 实际时间大于计划时间 判断是qpSn否有值
                        if (qpSn == null) {
                            tv_specified_learning.setText("学习");
                            tv_line_specified.setBackgroundResource(R.color.specified_red);
                            tv_yes.setText("未完成");
                            tv_yes.setBackgroundResource(R.drawable.shape_specified_nored);
                            tv_check.setVisibility(View.GONE);
                            tv_sign.setVisibility(View.GONE);
                            tv_actualChLength.setText(99 + "%");
                            tv_actualChLength.setTextColor(getActivity().getResources().getColor(R.color.bixue_red));
                        } else {
                            tv_specified_learning.setText("考试");
                            tv_line_specified.setBackgroundResource(R.color.holetotal_yellow);
                            tv_yes.setText("已完成");
                            tv_yes.setBackgroundResource(R.drawable.shape_specified_dai);
                            tv_check.setVisibility(View.GONE);
                            tv_sign.setVisibility(View.GONE);
                            tv_actualChLength.setText(100 + "%");
                            tv_actualChLength.setTextColor(getActivity().getResources().getColor(R.color.holetotal_yellow));
                        }

                    }
                }
            } else {//已完成

                final String signPicUrl = HelpUtil.judgeStrNull(rowsBean.getSignPicUrl());
                int checkMark = rowsBean.getCheckMark();
                if (qpSn == null) {
                    if (checkMark == 1 || checkMark == 2) {
                        if (signPicUrl.equals("")) {
                            tv_specified_learning.setVisibility(View.VISIBLE);
                            tv_specified_learning.setText("签名");
                        } else {
                            tv_specified_learning.setVisibility(View.VISIBLE);
                            tv_specified_learning.setText("查看签名");
                        }
                    } else if (checkMark == 0) {
                        if (finishMark == 1) {
                            tv_specified_learning.setVisibility(View.VISIBLE);
                            tv_specified_learning.setText("签名");
                        } else if (finishMark == 2) {
                            tv_specified_learning.setVisibility(View.VISIBLE);
                            tv_specified_learning.setText("查看签名");
                        }

                    }
                } else {
                    tv_specified_learning.setVisibility(View.VISIBLE);
                    tv_specified_learning.setText("考试");
                }
                tv_actualChLength.setText(100 + "%");
                tv_actualChLength.setTextColor(getActivity().getResources().getColor(R.color.holetotal_gray));
            }
            if (rowsBean.getSpotCheckMark() == 1) {
                tv_Spotcheck.setVisibility(View.VISIBLE);
                tv_Spotcheck.setText("通过抽查");
                tv_Spotcheck.setBackgroundResource(R.drawable.shape_specified_yes);
            } else if (rowsBean.getSpotCheckMark() == 2) {
                tv_Spotcheck.setVisibility(View.VISIBLE);
                tv_Spotcheck.setText("未通过抽查");
                tv_Spotcheck.setBackgroundResource(R.drawable.shape_specified_nored);
            } else {
                tv_Spotcheck.setVisibility(View.INVISIBLE);
            }


            if (rowsBean.getCheckMark() == 2) {
                tv_line_specified.setBackgroundResource(R.color.specified_red);
                tv_check.setVisibility(View.VISIBLE);
                tv_check.setText("审核未通过");
                tv_check.setBackgroundResource(R.drawable.shape_specified_nored);
                tv_yes.setText("已完成");
                tv_yes.setBackgroundResource(R.drawable.shape_specified_yes);
                tv_sign.setVisibility(View.VISIBLE);
                final String signPicUrl = HelpUtil.judgeStrNull(rowsBean.getSignPicUrl());
                if (signPicUrl.equals("")) {
                    tv_sign.setText("未签名");
                    tv_sign.setBackgroundResource(R.drawable.shape_specified_noorange);
                    //   tv_specified_learning.setText("签名");
                    tv_specified_learning.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isFastClicker()) {
                                return;
                            }

                            if (tv_specified_learning.getText().toString().trim().equals("签名")) {
                                int sn = rowsBean.getSn();
                                Bundle bundle = new Bundle();
                                bundle.putInt("sn", sn);
                                bundle.putString("signPicUrl", "");
                                bundle.putInt("signMark", 1);
                                startActivity(SignActivity.class, bundle);
                            } else if (tv_specified_learning.getText().toString().trim().equals("考试")) {
                                //代写逻辑

                            }

                        }
                    });

                } else {
                    tv_sign.setText("已签名");
                    tv_sign.setBackgroundResource(R.drawable.shape_specified_yes);
                    //    tv_specified_learning.setText("查看签名");

                    tv_specified_learning.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isFastClicker()) {
                                return;
                            }
                            if (tv_specified_learning.getText().toString().trim().equals("查看签名")) {
                                int sn = rowsBean.getSn();
                                Bundle bundle = new Bundle();
                                bundle.putInt("sn", sn);
                                bundle.putString("signPicUrl", signPicUrl);
                                bundle.putInt("signMark", 1);
                                startActivity(SignActivity.class, bundle);
                            } else if (tv_specified_learning.getText().toString().trim().equals("考试")) {

                                //代写逻辑

                            }
                        }
                    });

                }
            } else if (rowsBean.getCheckMark() == 1) {
                tv_line_specified.setBackgroundResource(R.color.holetotal_gray);
                tv_check.setVisibility(View.GONE);
                tv_yes.setText("已完成");
                tv_yes.setBackgroundResource(R.drawable.shape_specified_yes);
                tv_sign.setVisibility(View.VISIBLE);
                final String signPicUrl = HelpUtil.judgeStrNull(rowsBean.getSignPicUrl());
                if (signPicUrl.equals("")) {
                    tv_sign.setText("未签名");
                    tv_sign.setBackgroundResource(R.drawable.shape_specified_noorange);
                    tv_specified_learning.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isFastClicker()) {
                                return;
                            }
                            if (tv_specified_learning.getText().toString().trim().equals("签名")) {
                                int sn = rowsBean.getSn();
                                Bundle bundle = new Bundle();
                                bundle.putInt("sn", sn);
                                bundle.putString("signPicUrl", "");
                                bundle.putInt("signMark", 1);
                                startActivity(SignActivity.class, bundle);
                            } else if (tv_specified_learning.getText().toString().trim().equals("考试")) {
                                //代写逻辑
                            }

                        }
                    });
                } else {
                    tv_sign.setText("已签名");
                    tv_sign.setBackgroundResource(R.drawable.shape_specified_yes);
                    tv_specified_learning.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isFastClicker()) {
                                return;
                            }
                            if (tv_specified_learning.getText().toString().trim().equals("查看签名")) {
                                int sn = rowsBean.getSn();
                                Bundle bundle = new Bundle();
                                bundle.putInt("sn", sn);
                                bundle.putString("signPicUrl", signPicUrl);
                                bundle.putInt("signMark", 1);
                                startActivity(SignActivity.class, bundle);
                            } else if (tv_specified_learning.getText().toString().trim().equals("考试")) {
                                //代写逻辑
                            }
                        }
                    });
                }
            } else if (rowsBean.getCheckMark() == 0) {

                if (rowsBean.getFinishMark() == 1) {
                    tv_line_specified.setBackgroundResource(R.color.holetotal_yellow);
                    tv_yes.setText("已完成");
                    tv_yes.setBackgroundResource(R.drawable.shape_specified_yes);
                    tv_check.setVisibility(View.GONE);
                    tv_sign.setVisibility(View.VISIBLE);
                    tv_sign.setText("未签名");
                    tv_sign.setBackgroundResource(R.drawable.shape_specified_noorange);
                    //    tv_specified_learning.setText("签名");

                    tv_specified_learning.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isFastClicker()) {
                                return;
                            }
                            int sn = rowsBean.getSn();
                            Bundle bundle = new Bundle();
                            bundle.putInt("sn", sn);
                            bundle.putString("signPicUrl", "");
                            bundle.putInt("signMark", 1);
                            startActivity(SignActivity.class, bundle);
                        }
                    });

                } else if (rowsBean.getFinishMark() == 2) {
                    tv_line_specified.setBackgroundResource(R.color.holetotal_yellow);
                    tv_yes.setText("已完成");
                    tv_yes.setBackgroundResource(R.drawable.shape_specified_yes);
                    tv_check.setVisibility(View.GONE);
                    tv_sign.setVisibility(View.VISIBLE);
                    tv_sign.setText("已签名");
                    tv_sign.setBackgroundResource(R.drawable.shape_specified_yes);
                    tv_specified_learning.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isFastClicker()) {
                                return;
                            }
                            if (tv_specified_learning.getText().toString().trim().equals("查看签名")) {
                                int sn = rowsBean.getSn();
                                String signPicUrl = HelpUtil.judgeStrNull(rowsBean.getSignPicUrl());
                                Bundle bundle = new Bundle();
                                bundle.putInt("sn", sn);
                                bundle.putString("signPicUrl", signPicUrl);
                                bundle.putInt("signMark", 1);
                                startActivity(SignActivity.class, bundle);
                            } else if (tv_specified_learning.getText().toString().trim().equals("考试")) {
                                //代写逻辑
                            }
                        }
                    });

                } else if (rowsBean.getFinishMark() == 0) {
                    tv_specified_learning.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {//未完成
                            if (Utils.isFastClicker()) {
                                return;
                            }
                            if (tv_specified_learning.getText().toString().equals("学习")) {
                                SharedPreferences sp = getActivity().getSharedPreferences("CourseSupermarketBusinessFragment",
                                        Context.MODE_PRIVATE);
                                int photoMark = sp.getInt("photoMark", 0);
                                String photoUrl = sp.getString("photoUrl", "");
                                String orgId = sp.getString("orgId", "");
                                if (photoUrl.equals("")) {
                                    showTakePhotoPopupWindow();
                                    return;
                                }
                                int sn = rowsBean.getSn();
                                int setChLength = 0;
                                setChLength = (rowsBean.getMiNo() > rowsBean.getSetChLength()) ? rowsBean.getMiNo() : rowsBean.getSetChLength();
                                int checkMark = rowsBean.getCheckMark();
                                Integer qpSn = rowsBean.getQpSn();
                                Integer otpSn = HelpUtil.judgeIntNull(rowsBean.getOtpSn());
                                Bundle bundle = new Bundle();
                                bundle.putInt("photoMark", photoMark);
                                bundle.putInt("sn", sn);
                                bundle.putString("chName", chName);
                                bundle.putInt("setChLength", setChLength);
                                bundle.putInt("checkMark", checkMark);
                                bundle.putInt("finishMark", finishMark);
                                bundle.putString("photoUrl", photoUrl);
                                bundle.putString("orgId", orgId);
                                bundle.putInt("faceRecgMark", faceRecgMark);
                                bundle.putInt("chNo", chNo);
                                bundle.putInt("otpSn", otpSn);
                                bundle.putInt("minPer", minPer);
                                if (qpSn == null) {
                                    bundle.putInt("qpSn", 0);
                                } else {
                                    bundle.putInt("qpSn", qpSn);
                                }
                                startActivity(SubCourseLearnBusinessItemActivity.class, bundle);

                            } else if (tv_specified_learning.getText().toString().equals("考试")) {
                                //代写逻辑

                            }

                        }
                    });

                }
            }


        }

    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }
}
